package com.dz.syndata.service.impl;

import com.dz.syndata.bean.LoginBean;
import com.dz.syndata.bean.ResultDataBean;
import com.dz.syndata.bean.device.DeviceBean;
import com.dz.syndata.domain.server.Sys;
import com.dz.syndata.mapper.one.PrimaryDeviceMapper;
import com.dz.syndata.service.IDeviceInfoService;
import com.dz.syndata.util.HttpRequest;
import com.dz.syndata.utils.DateUtils;
import com.google.gson.Gson;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

@Service
public class DeviceInfoServiceImpl implements IDeviceInfoService {
    @Autowired
    PrimaryDeviceMapper primaryDeviceMapper;
    private List<String> lo;
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DeviceInfoServiceImpl.class);
    private String loginUrl = "http://10.97.69.133/dataService/service/publish/getToken?username=zhjyAdmin&password=ZAQ12wsx";
    //    private String dataUrl = "http://10.97.69.133/dataService/service/publish/structView/queryStructDataDetail?dataSetName=智慧经营分析系统&dataObjName=水情雨量数据接口&replaceHolder=11808020,2021-08-19 00:00:00,2021-08-19 23:59:00&page=0&limit=10";
    private String dataUrlDay = "http://10.97.69.133/dataService/service/publish/structView/queryStructDataDetail?dataSetName=智慧经营分析系统&dataObjName=水情日表数据接口&replaceHolder=";
    private String dataUrlOneHour = "http://10.97.69.133/dataService/service/publish/structView/queryStructDataDetail?dataSetName=智慧经营分析系统&dataObjName=水情小时表数据接口&replaceHolder=";
    private String page = "&page=0&limit=3";
    private LoginBean loginBean = null;
    private DeviceBean selectDeviceBean;

    /**
     * 获取日表数据
     *
     * @return
     */
    @Override
    public List<DeviceBean> selectRainfall() {
        logger.error("一天执行一次");
        String loginResult = HttpRequest.sendGet(loginUrl, null);
        loginBean = new Gson().fromJson(loginResult, LoginBean.class);
        logger.error(loginResult);
        List<DeviceBean> deviceBeans = primaryDeviceMapper.selectRainfallData();
        //todo
        if (loginBean != null && loginBean.getCode() == 200) {
            for (DeviceBean mDeviceBean : deviceBeans
            ) {
                if (mDeviceBean.getITEMID() != null) {
                    //todo
//                    String dataResult = " {\"msg\":\"操作成功\",\"total\":10,\"code\":200,\"data\":[{\"计算时段\":1,\"最大值标识\":1,\"最小值\":1287.32,\"最小值标识\":1,\"平均值\":1287.32,\"计算量点号\":11800121,\"操作值\":1287.32,\"数据质量标识\":1,\"时间\":\"2021-08-19 10:00:00.000\",\"最小值发生时间\":\"2021-08-19 10:00:00.000\",\"最大值发生时间\":\"2021-08-19 10:00:00.000\",\"最大值\":1287.32,\"平均值标识\":1},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":658.0,\"最小值标识\":1,\"平均值\":951.56,\"计算量点号\":11808020,\"操作值\":1565.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 09:00:00.000\",\"最小值发生时间\":\"2021-08-19 08:11:00.000\",\"最大值发生时间\":\"2021-08-19 08:59:00.000\",\"最大值\":1565.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":304.0,\"最小值标识\":1,\"平均值\":480.78,\"计算量点号\":11808020,\"操作值\":690.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 08:00:00.000\",\"最小值发生时间\":\"2021-08-19 07:16:00.000\",\"最大值发生时间\":\"2021-08-19 07:49:00.000\",\"最大值\":837.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":471.0,\"最小值标识\":1,\"平均值\":684.68,\"计算量点号\":11808020,\"操作值\":653.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 07:00:00.000\",\"最小值发生时间\":\"2021-08-19 06:01:00.000\",\"最大值发生时间\":\"2021-08-19 06:31:00.000\",\"最大值\":769.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":341.0,\"最小值标识\":1,\"平均值\":494.49,\"计算量点号\":11808020,\"操作值\":510.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 06:00:00.000\",\"最小值发生时间\":\"2021-08-19 05:01:00.000\",\"最大值发生时间\":\"2021-08-19 05:56:00.000\",\"最大值\":628.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":327.0,\"最小值标识\":1,\"平均值\":433.0,\"计算量点号\":11808020,\"操作值\":346.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 05:00:00.000\",\"最小值发生时间\":\"2021-08-19 04:56:00.000\",\"最大值发生时间\":\"2021-08-19 04:19:00.000\",\"最大值\":562.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":349.0,\"最小值标识\":1,\"平均值\":576.49,\"计算量点号\":11808020,\"操作值\":554.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 04:00:00.000\",\"最小值发生时间\":\"2021-08-19 03:01:00.000\",\"最大值发生时间\":\"2021-08-19 03:14:00.000\",\"最大值\":684.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":279.0,\"最小值标识\":1,\"平均值\":285.37,\"计算量点号\":11808020,\"操作值\":349.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 03:00:00.000\",\"最小值发生时间\":\"2021-08-19 02:06:00.000\",\"最大值发生时间\":\"2021-08-19 02:59:00.000\",\"最大值\":349.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":276.0,\"最小值标识\":1,\"平均值\":327.14,\"计算量点号\":11808020,\"操作值\":281.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 02:00:00.000\",\"最小值发生时间\":\"2021-08-19 01:24:00.000\",\"最大值发生时间\":\"2021-08-19 01:14:00.000\",\"最大值\":467.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":420.0,\"最小值标识\":1,\"平均值\":480.73,\"计算量点号\":11808020,\"操作值\":450.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 01:00:00.000\",\"最小值发生时间\":\"2021-08-19 00:04:00.000\",\"最大值发生时间\":\"2021-08-19 00:21:00.000\",\"最大值\":596.0,\"平均值标识\":2}]}\n";
                    String params = mDeviceBean.getITEMID() + "," + DateUtils.getDate() + " 00:00:00" + "," + DateUtils.getDate() + " 23:59:00";
                    String dataResult = HttpRequest.sendGet(dataUrlDay + params + page, null, loginBean.getData());
                    logger.error("日表数据接口:" + dataResult);
                    ResultDataBean resultDataBean = new Gson().fromJson(dataResult, ResultDataBean.class);
                    if (resultDataBean.getCode() == 200) {
                        DeviceBean selectDeviceBean = primaryDeviceMapper.selectMonthItem(mDeviceBean);
                        if (resultDataBean.getTotal() != 0) {
                            ResultDataBean.DataBean dataBean = resultDataBean.getData().get(0);
                            DeviceBean deviceBean = new DeviceBean();
                            deviceBean.setITEMID(dataBean.get计算量点号() + "");
                            deviceBean.setITEMVALUE(dataBean.get平均值() + "");
                            deviceBean.setITEMCODE(mDeviceBean.getCode());
                            deviceBean.setORGCODE("PSSD");
                            deviceBean.setITEMDATE(DateUtils.yesterdayDate(DateUtils.getStringDD(dataBean.get时间())));
//                            deviceBean.setITEMDATE(DateUtils.getStringDD(DateUtils.yesterdayDate()));
                            if (selectDeviceBean == null || DateUtils.compareTo
                                    (DateUtils.getStringDD(deviceBean.getITEMDATE()), DateUtils.getStringDD(selectDeviceBean.getITEMDATE())) > 0) {
                                primaryDeviceMapper.insertRainfall(deviceBean);
                                logger.error("雨量数据插入成功->" + mDeviceBean.getCode());
                                logger.error("雨量数据插入时间" + deviceBean.getITEMDATE() + ",值：" + deviceBean.getITEMVALUE());
                            } else {
                                logger.error("雨量更新时间与入库时间重合->" + mDeviceBean.getCode());
                            }

                        } else {
                            InsertRainData(mDeviceBean, DateUtils.yesterdayDate(DateUtils.getDate()));
                        }
                    } else {
                        InsertRainData(mDeviceBean, DateUtils.yesterdayDate(DateUtils.getDate()));
                        /**
                         * 参数请求出错，错误后继续请求
                         */
                        continue;
                    }
                }
            }


        }


        return null;
    }

    /**
     * 获取小时表数据
     *
     * @param reconId
     * @param token
     * @return
     */
    private DeviceBean getDataFlow(String reconId, String token) {
        String params = reconId + "," + DateUtils.getDate() + " 00:00:00" + "," + DateUtils.getDate() + " 23:59:00";
        String dataResult = HttpRequest.sendGet(dataUrlOneHour + params + page, null, token);
        ResultDataBean resultDataBean = new Gson().fromJson(dataResult, ResultDataBean.class);
        logger.error("小时表数据接口:" + dataResult);
        if (resultDataBean.getCode() == 200) {
            if (resultDataBean.getData().size() != 0) {

                ResultDataBean.DataBean dataBean = resultDataBean.getData().get(0);
                DeviceBean deviceBean = new DeviceBean();
                deviceBean.setITEMID(dataBean.get计算量点号() + "");
                deviceBean.setITEMVALUE(dataBean.get最大值() + "");
                deviceBean.setUpdateTime(DateUtils.getStringTime(dataBean.get时间()));
                return deviceBean;

            }
        }


        return null;

    }

    @Override
    public Integer updateById(DeviceBean deviceBean) {
        return null;
    }

    @Override
    public Integer insertData() {
        //todo
//        DeviceBean deviceBean1=new DeviceBean();
//        deviceBean1.setHCL("112");
//        deviceBean1.setRKLL("25.0");
//        deviceBean1.setORGCODE("PSSD");
//        deviceBean1.setUpdateTime("2021-08-19 20:00:00");
//        System.out.println("22");

//        primaryDeviceMapper.insertData(deviceBean1);
        String loginResult = HttpRequest.sendGet(loginUrl, null);
        loginBean = new Gson().fromJson(loginResult, LoginBean.class);
        logger.error(loginResult);
        //获取出库
        String inStorageId = "11808041";
        //全厂有功
        String activePowerId = "11808020";
        if (loginBean != null) {
            //出库
            DeviceBean inStorageData = getDataFlow(inStorageId, loginBean.getData());
            //有功
            DeviceBean activePowerData = getDataFlow(activePowerId, loginBean.getData());
            if (inStorageData != null && activePowerData != null) {
                DeviceBean selectDeviceBean = primaryDeviceMapper.selectHourPlan();

                DeviceBean deviceBean = new DeviceBean();
                String YYYY_mm_ddHour = DateUtils.getStringYYYY_MM_DD(inStorageData.getUpdateTime());
                deviceBean.setUpdateTime(DateUtils.getStringTime(YYYY_mm_ddHour + ":00:00"));
                deviceBean.setRKLL(inStorageData.getITEMVALUE());
                /**
                 * 有功转换成万千瓦,在以前基础上除10
                 * (更新)
                 */
                deviceBean.setHCL(String.valueOf(Double.parseDouble(activePowerData.getITEMVALUE()) / 10));
                deviceBean.setORGCODE("PSSD");
                if (selectDeviceBean == null || DateUtils.compareTo(DateUtils.getStringTime(deviceBean.getUpdateTime()),
                        DateUtils.getStringTime(selectDeviceBean.getTIMEID())) > 0) {
                    primaryDeviceMapper.insertData(deviceBean);
                    logger.error("出库流量和全厂有功数据插入成功");
                } else {
                    logger.error("出库流量和全厂有功数入库时间重合");
                }

            }
        }
        return 0;
    }

    /**
     * 通过请求接口更新数据
     *
     * @param date
     */
    @Override
    public void updateRainfall(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date sDate = null;
        try {
            sDate = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long datePoorDay = DateUtils.getDatePoorDay(DateUtils.getNowDate(), sDate);

        String loginResult = HttpRequest.sendGet(loginUrl, null);
        loginBean = new Gson().fromJson(loginResult, LoginBean.class);

        List<DeviceBean> deviceBeans = primaryDeviceMapper.selectRainfallData();
        if (loginBean != null && loginBean.getCode() == 200) {
            for (int i = 0; i <= datePoorDay; i++) {
                String startTime = DateUtils.addDay(date, i);
                for (DeviceBean mDeviceBean : deviceBeans) {
                    if (mDeviceBean.getITEMID() != null) {
                        //todo
//                    String dataResult = " {\"msg\":\"操作成功\",\"total\":10,\"code\":200,\"data\":[{\"计算时段\":1,\"最大值标识\":1,\"最小值\":1287.32,\"最小值标识\":1,\"平均值\":1287.32,\"计算量点号\":11800121,\"操作值\":1287.32,\"数据质量标识\":1,\"时间\":\"2021-08-19 10:00:00.000\",\"最小值发生时间\":\"2021-08-19 10:00:00.000\",\"最大值发生时间\":\"2021-08-19 10:00:00.000\",\"最大值\":1287.32,\"平均值标识\":1},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":658.0,\"最小值标识\":1,\"平均值\":951.56,\"计算量点号\":11808020,\"操作值\":1565.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 09:00:00.000\",\"最小值发生时间\":\"2021-08-19 08:11:00.000\",\"最大值发生时间\":\"2021-08-19 08:59:00.000\",\"最大值\":1565.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":304.0,\"最小值标识\":1,\"平均值\":480.78,\"计算量点号\":11808020,\"操作值\":690.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 08:00:00.000\",\"最小值发生时间\":\"2021-08-19 07:16:00.000\",\"最大值发生时间\":\"2021-08-19 07:49:00.000\",\"最大值\":837.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":471.0,\"最小值标识\":1,\"平均值\":684.68,\"计算量点号\":11808020,\"操作值\":653.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 07:00:00.000\",\"最小值发生时间\":\"2021-08-19 06:01:00.000\",\"最大值发生时间\":\"2021-08-19 06:31:00.000\",\"最大值\":769.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":341.0,\"最小值标识\":1,\"平均值\":494.49,\"计算量点号\":11808020,\"操作值\":510.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 06:00:00.000\",\"最小值发生时间\":\"2021-08-19 05:01:00.000\",\"最大值发生时间\":\"2021-08-19 05:56:00.000\",\"最大值\":628.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":327.0,\"最小值标识\":1,\"平均值\":433.0,\"计算量点号\":11808020,\"操作值\":346.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 05:00:00.000\",\"最小值发生时间\":\"2021-08-19 04:56:00.000\",\"最大值发生时间\":\"2021-08-19 04:19:00.000\",\"最大值\":562.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":349.0,\"最小值标识\":1,\"平均值\":576.49,\"计算量点号\":11808020,\"操作值\":554.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 04:00:00.000\",\"最小值发生时间\":\"2021-08-19 03:01:00.000\",\"最大值发生时间\":\"2021-08-19 03:14:00.000\",\"最大值\":684.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":279.0,\"最小值标识\":1,\"平均值\":285.37,\"计算量点号\":11808020,\"操作值\":349.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 03:00:00.000\",\"最小值发生时间\":\"2021-08-19 02:06:00.000\",\"最大值发生时间\":\"2021-08-19 02:59:00.000\",\"最大值\":349.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":276.0,\"最小值标识\":1,\"平均值\":327.14,\"计算量点号\":11808020,\"操作值\":281.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 02:00:00.000\",\"最小值发生时间\":\"2021-08-19 01:24:00.000\",\"最大值发生时间\":\"2021-08-19 01:14:00.000\",\"最大值\":467.0,\"平均值标识\":2},{\"计算时段\":1,\"最大值标识\":1,\"最小值\":420.0,\"最小值标识\":1,\"平均值\":480.73,\"计算量点号\":11808020,\"操作值\":450.0,\"数据质量标识\":1,\"时间\":\"2021-08-19 01:00:00.000\",\"最小值发生时间\":\"2021-08-19 00:04:00.000\",\"最大值发生时间\":\"2021-08-19 00:21:00.000\",\"最大值\":596.0,\"平均值标识\":2}]}\n";
                        String params = mDeviceBean.getITEMID() + "," + startTime + " 00:00:00" + "," + startTime + " 23:59:00";
                        String dataResult = HttpRequest.sendGet(dataUrlDay + params + page, null, loginBean.getData());
                        logger.error("http请求->雨量据接口:" + dataResult);
                        ResultDataBean resultDataBean = new Gson().fromJson(dataResult, ResultDataBean.class);
                        if (resultDataBean.getCode() == 200) {
                            mDeviceBean.setITEMDATE(DateUtils.yesterdayDate(DateUtils.getStringDD(startTime)));
                            List<DeviceBean> deviceBeans1 = primaryDeviceMapper.selectMonthItemAll(mDeviceBean);
                            if (resultDataBean.getTotal() != 0) {
                                ResultDataBean.DataBean dataBean = resultDataBean.getData().get(0);
                                DeviceBean deviceBean = new DeviceBean();
                                deviceBean.setITEMID(dataBean.get计算量点号() + "");
                                deviceBean.setITEMVALUE(dataBean.get平均值() + "");
                                deviceBean.setITEMCODE(mDeviceBean.getCode());
                                deviceBean.setORGCODE("PSSD");
                                deviceBean.setITEMDATE(DateUtils.yesterdayDate(DateUtils.getStringDD(dataBean.get时间())));
                                if (deviceBeans1.size() > 0) {
                                    logger.error("http请求雨量数据日期已存在->" + mDeviceBean.getCode() + "-时间->" + DateUtils.getStringDD(deviceBean.getITEMDATE()));

                                } else {
                                    primaryDeviceMapper.insertRainfall(deviceBean);
                                    logger.error("http请求雨量数据插入成功->" + mDeviceBean.getCode() + "请求时间->"
                                            + DateUtils.getStringDD(startTime) + "-最后更新时间->" +
                                            DateUtils.getStringDD(deviceBean.getITEMDATE()));

                                }

                            } else {
                                InsertRainData(mDeviceBean, DateUtils.yesterdayDate(startTime));
                            }
                        } else {
                            /**
                             * 参数请求出错，错误后继续请求
                             */
                            continue;
                        }
                    }

                }
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


            }


        }

    }

    /**
     * 插入雨量数据
     *
     * @author
     */
    public synchronized void InsertRainData(DeviceBean mDeviceBean, String startTime) {
        DeviceBean deviceBean = new DeviceBean();
        deviceBean.setITEMID(mDeviceBean.getITEMID() + "");
        deviceBean.setITEMVALUE("0");
        deviceBean.setITEMCODE(mDeviceBean.getCode());
        deviceBean.setORGCODE("PSSD");

        deviceBean.setITEMDATE(startTime);
        primaryDeviceMapper.insertRainfall(deviceBean);
        logger.error("雨量数据插入成功->结果集:" + 0 + "编码code->" + mDeviceBean.getCode() + "最后更新时间:->" + startTime);
    }
}
